﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using fess.Screens;
using fess.Maps;

namespace fess.Graphics
{
  static class FXResources
  {
    public static FXRes smallHit1, smallHit2;
    public static FXRes blood, fire1, fire2;
  }

  class FXRes
  {
    SpriteSheet sheet;
    TimeSpan duration;

    public FXRes(SpriteSheet sheet, TimeSpan duration)
    {
      this.sheet = sheet;
      this.duration = duration;
    }

    public void Draw(SpriteBatch batch, Vector2 place, float angle, Rectangle view, int frame, Color color, float depth = 0, float scale = 1)
    {
      sheet.Draw(batch, frame % sheet.Width, frame / sheet.Width, place, view, angle, depth, scale, color);
    }

    public int Frames { get { return sheet.Width * sheet.Height; } }

    public double Duration { get { return duration.TotalSeconds; } }
  }

  class FX : IDrawable
  {
    FXRes fx;
    TimeSpan appear;
    Vector2 place;
    float angle, scale, depth;
    Color color = Color.White;

    public FX(FXRes fx, Vector2 place, float angle, float scale, TimeSpan appearTime)
    {
      this.fx = fx;
      this.appear = appearTime;
      this.place = place;
      this.angle = angle;
      this.scale = scale;
      depth = 0;
    }

    public FX(FXRes fx, Vector2 place, float angle, float scale, TimeSpan appear, Color color)
    {
      this.fx = fx;
      this.appear = appear;
      this.place = place;
      this.angle = angle;
      this.scale = scale;
      this.color = color;
      depth = 0;
    }

    public bool Draw(SpriteBatch batch, Rectangle view, TimeSpan time)
    {
      float t = (float)((time - appear).TotalSeconds / fx.Duration);
      if (t >= 0 && t <= 1)
      {
        int frame = Math.Min(fx.Frames - 1, (int)Math.Floor(fx.Frames * t));
        fx.Draw(batch, place, angle, view, frame, color, depth, scale);
      }

      return t < 1;
    }
  }

  class BloodSplatter : IDrawable
  {
    FXRes fx;
    Vector2 currentPos, initialPos, direction;
    TimeSpan appear;
    PassMap map;
    int variant;
    float depth, scale, rotation;
    Color color;
    bool expand;

    public BloodSplatter(FXRes sheet, Vector2 flyFrom, Vector2 direction, TimeSpan time, PassMap map, Color color, bool expand, float scale = 0)
    {
      this.fx = sheet;
      this.currentPos = flyFrom;
      this.initialPos = flyFrom;
      this.direction = direction;
      this.appear = time;
      this.map = map;
      this.color = color;
      this.expand = expand;
      variant = Misc.Rand.Next(sheet.Frames);
      depth = VisResources.GroundMarksDepth - (float)Misc.Rand.NextDouble() * 0.0001f;
      this.scale = scale == 0 ? (float)Misc.Rand.NextDouble() * 0.5f + 0.1f : scale;
      rotation = (float)(Math.PI * 2 * Misc.Rand.NextDouble());
    }

    public bool Draw(SpriteBatch batch, Rectangle view, TimeSpan time)
    {
      float t = (float)((time - appear).TotalSeconds / fx.Duration);
      if (t < 0)
        return true;

      if (t < 1)
        currentPos = map.MoveObject(currentPos, initialPos + t * direction, 5);
              
      fx.Draw(batch, currentPos, rotation, view, variant, color, depth, scale * (expand ? Math.Min(1.3f, (1 + t / 30)) : 1));

      return true;
    }
  }

}
